package cn.net.Thread;
/*
测试线程池ExecutorService
线程池的工具类：Executors
创建多个线程池：newFixedThreadPool(n)
 */
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TestExecutor {
    public static void main(String[] args) {
        saleTickets t = new saleTickets();//第三步
        Thread t1 = new Thread(t);//6.将目标对象与Thread线程对象绑定
        Thread t2 = new Thread(t);
        Thread t3 = new Thread(t);
        Thread t4 = new Thread(t);
        t1.start();
        t2.start();
        t3.start();
        t4.start();
        /**线程池ExecutorService:用来存储线程的池子,把新建线程/启动线程/关闭线程的任务都交给池来管理*/
        /**Executors用来辅助创建线程池对象,
         * newFixedThreadPool(n)创建具有参数个数的线程数的线程池*///第四步、
        ExecutorService pool = Executors.newFixedThreadPool(5);//7.最多5个线程
        for (int i = 0; i <5 ; i++) {//8.
            //执行任务excute(),每次调用都会启动一个线程
                pool.execute(t);//9.本方法的参数就是执行的业务(t),也就是实现类的目标对象(t)
        }

    }
}
class saleTickets implements Runnable {//第一步
static  int tickets =100;//1.
    @Override//第二步
    public void run() {
        while (true){ //2，
            try {
                Thread.sleep(10);//3.
            } catch (InterruptedException e) {
                e.printStackTrace();
            }//4.Thread.currentThread().getName()
            System.out.println(Thread.currentThread().getName()+"="+tickets--);
            if(tickets<=0) break;//5.
        }
    }
}